<?php

class Parser_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Registry::get('layout')->setLayout('duty');
        
        $activeResource=Parser_Model_Parser::RESOURCE_NAME;
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
        
        ini_set('max_execution_time',0);
    }

    public function indexAction()
    {
        if (!Zend_Registry::get('user')->canView())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $form=new Parser_Form_Parser();
        
        $request=$this->getRequest();
        
        if ($request->isPost())
        {
            if ($form->isValid($request->getPost()))
            {
                $this->tagAction($form->getValue('text'));
            }
        }
        
        $this->view->form=$form;
    }

    public function tagAction($tg='')
    {
        $tag=(string) !empty($tg) ? $tg : $this->_getParam('tag');
        
        if (!Zend_Registry::get('user')->canView() || empty($tag))
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        if (preg_match('/[^a-zа-я\x20\-_]/iu',$tag)) return;

        $output='';

        $nodeTbl=new Application_Model_NodeDb();
        $tagsTbl=new Application_Model_TagsDb();
        
        $select=Zend_Registry::get('db')->select()->from('nodes',array('max'=>'MAX(id)'));
        $st=$select->query();
        $result=$st->fetch();

        for ($id=1;$id<=$result['max'];$id++)
        {
            $node=$nodeTbl->fetchNodeById($id);
            
            if (!$node->isExists()) continue;
                
            $parser=new Parser_Model_Parser();
            
            $text=$node->full_text;
            $count=$parser->parse($tag,$text);
            $node->full_text=$text;
            
            if ($count>0)
            {
                $output.='<li><a href="'.$this->view->fixUrl('node/id/'.$node->id).'">'.$node->title.'</a></li>';
        
                // saving tag
                $tagObj=new Application_Model_Tags(array(
                                                    'tag' => $tag,
                                                    'nid' => $node->id,
                                                    'url_alias' => $node->url_alias,
                                                    'language' => $node->language
                                                    ));
                
                $tagsTbl->save($tagObj);
                
                // seting node keywords
                $keywords=$node->getKeywords();
                
                if (empty($keywords)) $keywords=$tag;
                else {
                    $keywords.=', '.$tag;
                }
                
                $node->setKeywords($keywords);
                
                $nodeTbl->save($node);
            }
        }
        
        if (empty($output)) $output.='<li>:(</li>';
        
        $this->view->output='<ul>'.$output.'</ul>';
    }
}

